package routes

import (
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	_ "gitee.com/cow111023/ginblog/docs"
	"github.com/gin-contrib/multitemplate"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func createMyRender() multitemplate.Renderer {
	p := multitemplate.NewRenderer()
	p.AddFromFiles("admin", "static/admin/index.html")
	p.AddFromFiles("front", "static/front/index.html")
	return p
}
func InitRouter() {
	gin.SetMode(utils.AppMode)
	r := gin.New()
	r.HTMLRender = createMyRender()
	r.Use(gin.Recovery(), middleware.Logger(), middleware.Cors())
	//后台管理页面
	//r.LoadHTMLGlob("static/admin/index.html")
	//r.LoadHTMLGlob("static/front/index.html")
	r.Static("/static", "./static/front/static")
	r.Static("/admin", "./static/admin")
	r.StaticFile("/favicon.ico", "static/front/favicon.ico")
	r.GET("/", func(c *gin.Context) {
		c.HTML(200, "front", nil)
	})
	r.GET("/admin", func(c *gin.Context) {
		c.HTML(200, "admin", nil)
	})
	//go:generate
	// 设置本地Swagger文档路径
	r.GET("/swagger/*any", ginSwagger.DisablingWrapHandler(
		ginSwagger.URL("/docs/swagger.yaml"), // Swagger 文档路径
		"NAME_OF_ENV_VARIABLE",
	))

	// 其他路由设置
	auth := r.Group("/api/v1")
	auth.Use(middleware.JwtToken())
	{
		//用户模块的路由接口
		auth.PUT("user/:id", v1.UpdateUser)
		auth.DELETE("user/:id", v1.DeleteUser)
		//分类模块的路由接口
		auth.POST("category/add", v1.AddCategory)

		auth.PUT("category/:id", v1.UpdateCategory)
		auth.DELETE("category/:id", v1.DeleteCategory)
		//文章模块的路由接口
		auth.POST("article/add", v1.AddArticle)
		auth.PUT("article/:id", v1.UpdateArticle)
		auth.DELETE("article/:id", v1.DeleteArticle)
		//上传文件
		auth.POST("upload", v1.Upload)

	}
	router := r.Group("/api/v1")
	{

		router.POST("user/add", v1.AddUser)
		router.GET("users", v1.GetUserList)
		router.GET("category", v1.GetCateList)
		router.GET("category/:id", v1.GetArtInfo)
		router.GET("article", v1.GetArticleList)
		router.GET("article/list/:id", v1.GetCateArticle)
		router.GET("article/info/:id", v1.GetArtInfo)
		router.POST("login", v1.Login)
		// 登录控制模块
		router.POST("loginfront", v1.LoginFront)
		// 评论模块
		router.POST("addcomment", v1.AddComment)
		router.GET("comment/info/:id", v1.GetComment)
		router.GET("comment/list", v1.GetCommentList)
		router.GET("commentfront/:id", v1.GetCommentListFront)
		router.GET("commentcount/:id", v1.GetCommentCount)
		router.PUT("checkcomment/:id", v1.Checkcomment)
		router.DELETE("delcomment/:id", v1.DeleteComment)

	}
	_ = r.Run(utils.HttpPort)
}
